home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / res / res_debug.c < prev    next >
C/C++ Source or Header  |  1995-12-22  |  19KB  |  814 lines

  1. /*
  2.  * ++Copyright++ 1985, 1990, 1993
  3.  * -
  4.  * Copyright (c) 1985, 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *     This product includes software developed by the University of
  18.  *     California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  * 
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  * 
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  * 
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #if defined(LIBC_SCCS) && !defined(lint)
  57. static char sccsid[] = "@(#)res_debug.c    8.1 (Berkeley) 6/4/93";
  58. static char rcsid[] = "$Id: res_debug.c,v 8.7 1995/12/22 10:20:39 vixie Exp $";
  59. #endif /* LIBC_SCCS and not lint */
  60.  
  61. #include <sys/param.h>
  62. #include <netinet/in.h>
  63. #include <arpa/inet.h>
  64. #include <arpa/nameser.h>
  65.  
  66. #include <stdio.h>
  67. #include <netdb.h>
  68. #include <resolv.h>
  69. #if defined(BSD) && (BSD >= 199103)
  70. # include <string.h>
  71. #else
  72. # include "../conf/portability.h"
  73. #endif
  74.  
  75. #if defined(USE_OPTIONS_H)
  76. # include "../conf/options.h"
  77. #endif
  78.  
  79. extern const char *_res_opcodes[];
  80. extern const char *_res_resultcodes[];
  81.  
  82. /* XXX: we should use getservbyport() instead. */
  83. static const char *
  84. dewks(wks)
  85.     int wks;
  86. {
  87.     static char nbuf[20];
  88.  
  89.     switch (wks) {
  90.     case 5: return "rje";
  91.     case 7: return "echo";
  92.     case 9: return "discard";
  93.     case 11: return "systat";
  94.     case 13: return "daytime";
  95.     case 15: return "netstat";
  96.     case 17: return "qotd";
  97.     case 19: return "chargen";
  98.     case 20: return "ftp-data";
  99.     case 21: return "ftp";
  100.     case 23: return "telnet";
  101.     case 25: return "smtp";
  102.     case 37: return "time";
  103.     case 39: return "rlp";
  104.     case 42: return "name";
  105.     case 43: return "whois";
  106.     case 53: return "domain";
  107.     case 57: return "apts";
  108.     case 59: return "apfs";
  109.     case 67: return "bootps";
  110.     case 68: return "bootpc";
  111.     case 69: return "tftp";
  112.     case 77: return "rje";
  113.     case 79: return "finger";
  114.     case 87: return "link";
  115.     case 95: return "supdup";
  116.     case 100: return "newacct";
  117.     case 101: return "hostnames";
  118.     case 102: return "iso-tsap";
  119.     case 103: return "x400";
  120.     case 104: return "x400-snd";
  121.     case 105: return "csnet-ns";
  122.     case 109: return "pop-2";
  123.     case 111: return "sunrpc";
  124.     case 113: return "auth";
  125.     case 115: return "sftp";
  126.     case 117: return "uucp-path";
  127.     case 119: return "nntp";
  128.     case 121: return "erpc";
  129.     case 123: return "ntp";
  130.     case 133: return "statsrv";
  131.     case 136: return "profile";
  132.     case 144: return "NeWS";
  133.     case 161: return "snmp";
  134.     case 162: return "snmp-trap";
  135.     case 170: return "print-srv";
  136.     default: (void) sprintf(nbuf, "%d", wks); return (nbuf);
  137.     }
  138. }
  139.  
  140. /* XXX: we should use getprotobynumber() instead. */
  141. static const char *
  142. deproto(protonum)
  143.     int protonum;
  144. {
  145.     static char nbuf[20];
  146.  
  147.     switch (protonum) {
  148.     case 1: return "icmp";
  149.     case 2: return "igmp";
  150.     case 3: return "ggp";
  151.     case 5: return "st";
  152.     case 6: return "tcp";
  153.     case 7: return "ucl";
  154.     case 8: return "egp";
  155.     case 9: return "igp";
  156.     case 11: return "nvp-II";
  157.     case 12: return "pup";
  158.     case 16: return "chaos";
  159.     case 17: return "udp";
  160.     default: (void) sprintf(nbuf, "%d", protonum); return (nbuf);
  161.     }
  162. }
  163.  
  164. static const u_char *
  165. do_rrset(msg, len, cp, cnt, pflag, file, hs)
  166.     int cnt, pflag, len;
  167.     const u_char *cp, *msg;
  168.     const char *hs;
  169.     FILE *file;
  170. {
  171.     int n;
  172.     int sflag;
  173.  
  174.     /*
  175.      * Print answer records.
  176.      */
  177.     sflag = (_res.pfcode & pflag);
  178.     if (n = ntohs(cnt)) {
  179.         if ((!_res.pfcode) ||
  180.             ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
  181.             fprintf(file, hs);
  182.         while (--n >= 0) {
  183.             if ((!_res.pfcode) || sflag) {
  184.                 cp = p_rr(cp, msg, file);
  185.             } else {
  186.                 unsigned int dlen;
  187.                 cp += __dn_skipname(cp, cp + MAXCDNAME);
  188.                 cp += INT16SZ;
  189.                 cp += INT16SZ;
  190.                 cp += INT32SZ;
  191.                 dlen = _getshort((u_char*)cp);
  192.                 cp += INT16SZ;
  193.                 cp += dlen;
  194.             }
  195.             if ((cp - msg) > len)
  196.                 return (NULL);
  197.         }
  198.         if ((!_res.pfcode) ||
  199.             ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
  200.             putc('\n', file);
  201.     }
  202.     return (cp);
  203. }
  204.  
  205. void
  206. __p_query(msg)
  207.     const u_char *msg;
  208. {
  209.     __fp_query(msg, stdout);
  210. }
  211.  
  212. #ifdef ultrix
  213. /* ultrix 4.0's packaging has some icky packaging.  alias for it here.
  214.  * there is more junk of this kind over in res_comp.c.
  215.  */
  216. void
  217. p_query(msg)
  218.     const u_char *msg;
  219. {
  220.     __p_query(msg);
  221. }
  222. #endif
  223.  
  224. /*
  225.  * Print the current options.
  226.  * This is intended to be primarily a debugging routine.
  227.  */
  228. void
  229. __fp_resstat(statp, file)
  230.     struct __res_state *statp;
  231.     FILE *file;
  232. {
  233.     register u_long mask;
  234.  
  235.     fprintf(file, ";; res options:");
  236.     if (!statp)
  237.         statp = &_res;
  238.     for (mask = 1;  mask != 0;  mask <<= 1)
  239.         if (statp->options & mask)
  240.             fprintf(file, " %s", p_option(mask));
  241.     putc('\n', file);
  242. }
  243.  
  244. /*
  245.  * Print the contents of a query.
  246.  * This is intended to be primarily a debugging routine.
  247.  */
  248. void
  249. __fp_nquery(msg, len, file)
  250.     const u_char *msg;
  251.     int len;
  252.     FILE *file;
  253. {
  254.     register const u_char *cp, *endMark;
  255.     register const HEADER *hp;
  256.     register int n;
  257.  
  258.     if ((_res.options & RES_INIT) == 0 && res_init() == -1)
  259.         return;
  260.  
  261. #define TruncTest(x) if (x >= endMark) goto trunc
  262. #define    ErrorTest(x) if (x == NULL) goto error
  263.  
  264.     /*
  265.      * Print header fields.
  266.      */
  267.     hp = (HEADER *)msg;
  268.     cp = msg + HFIXEDSZ;
  269.     endMark = cp + len;
  270.     if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) {
  271.         fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
  272.             _res_opcodes[hp->opcode],
  273.             _res_resultcodes[hp->rcode],
  274.             ntohs(hp->id));
  275.         putc('\n', file);
  276.     }
  277.     if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))
  278.         putc(';', file);
  279.     if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
  280.         fprintf(file, "; flags:");
  281.         if (hp->qr)
  282.             fprintf(file, " qr");
  283.         if (hp->aa)
  284.             fprintf(file, " aa");
  285.         if (hp->tc)
  286.             fprintf(file, " tc");
  287.         if (hp->rd)
  288.             fprintf(file, " rd");
  289.         if (hp->ra)
  290.             fprintf(file, " ra");
  291.     }
  292.     if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {
  293.         fprintf(file, "; Ques: